package com.dwms.exception;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.dwms.entity.Result;
import com.dwms.enums.ResultCode;
import com.dwms.utils.SendEmail;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务处理的异常
     * @param e
     * @return
     */
    @ExceptionHandler(BizException.class)
    @ResponseBody
    public Result handleBizException(BizException e){
        log.error("处理业务处理的异常:handleBizException:" + ExceptionUtil.stacktraceToString(e));
        log.error("处理业务处理的异常:e===" + ExceptionUtil.stacktraceToString(e));
        SendEmail.sendEmail(ExceptionUtil.getSimpleMessage(e),ExceptionUtil.stacktraceToString(e));
        return Result.failed(ExceptionUtil.getSimpleMessage(e));
    }

    /**
     * 处理程序内部的异常
     * @param e
     * @return
     */
    @ExceptionHandler(ServerException.class)
    @ResponseBody
    public Result handleServerException(ServerException e){
        log.error("处理程序内部的异常:handleServerException:" + ExceptionUtil.stacktraceToString(e));
        log.error("处理程序内部的异常:e===" + ExceptionUtil.stacktraceToString(e));
        SendEmail.sendEmail(ExceptionUtil.getSimpleMessage(e),ExceptionUtil.stacktraceToString(e));
        return Result.failed(ResultCode.SERVER_EXCEPTION,"系统太繁忙，请稍后再试");
    }

    /**
     * 处理feign接口的异常
     * @param e
     * @return
     */
    @ExceptionHandler(FeignException.class)
    @ResponseBody
    public Result handleFeignException(FeignException e){
        log.error("处理feign接口的异常:handleFeignException:" + ExceptionUtil.stacktraceToString(e));
        log.error("处理feign接口的异常:e===" + ExceptionUtil.stacktraceToString(e));
        SendEmail.sendEmail(ExceptionUtil.getSimpleMessage(e),ExceptionUtil.stacktraceToString(e));
        return Result.failed(ResultCode.SERVER_EXCEPTION,"系统太繁忙，请稍后再试");
    }

    /**
     * 处理Hystrix接口的异常
     * @param e
     * @return
     */
    @ExceptionHandler(HystrixRuntimeException.class)
    @ResponseBody
    public Result handleHystrixException(FeignException e){
        log.error("处理Hystrix的异常:handleHystrixException:" + ExceptionUtil.stacktraceToString(e));
        log.error("处理Hystrix的异常:e===" + ExceptionUtil.stacktraceToString(e));
        SendEmail.sendEmail(ExceptionUtil.getSimpleMessage(e),ExceptionUtil.stacktraceToString(e));
        return Result.failed(ResultCode.SERVER_EXCEPTION,"系统太繁忙，请稍后再试");
    }

    /**
     * 处理所有不可知的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleException(Exception e){
        log.error("处理所有不可知的异常:handleException:" + ExceptionUtil.stacktraceToString(e));
        log.error("处理所有不可知的异常:e===" + ExceptionUtil.stacktraceToString(e));
        SendEmail.sendEmail(ExceptionUtil.getSimpleMessage(e),ExceptionUtil.stacktraceToString(e));
        return Result.failed("系统太繁忙，请稍后再试");
    }
}
